package com.china08.yxyapi.api.manage.stu.myclass.moments;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMomentsComment;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsCommentRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 班级圈--家长--显示评论
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class MomentsStuCommentController extends YxyApiBaseController {

    @Autowired
    private YxyMomentsCommentRepository yxyMomentsCommentRepository;

    /* @ApiOperation("家长端班级圈") */
    @RequestMapping(value = "/api_yw/manage/stu/moments/momentsStuComment/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {
        String currUserId = getProfile(request, response).getId();

        List<RespModel> returnList = new ArrayList<>();

        List<YxyMomentsComment> yxyMomentsCommentLst = yxyMomentsCommentRepository.findByMomentsId(id);

        for (YxyMomentsComment respModel : yxyMomentsCommentLst) {
            RespModel model = new RespModel();

            model.id = respModel.getId();
            model.momentsId = respModel.getMomentsId();
            model.userId = respModel.getUserId();
            model.userNick = respModel.getUserNick();
            model.avatar = respModel.getAvatar();
            model.replyerNick = respModel.getReplyerNick();
            System.err.println(model.replyerNick);
            model.comment = respModel.getComment();
            Date date = respModel.getCreatedDate();
            model.dateFormat = date != null ? DateUtils.format(date) : "";

            // 是否自己的评论 （前端以此判断是否显示删除）
            model.ownComment = StringUtils.equals(currUserId, model.userId);
            returnList.add(model);

        }
        return returnList;
    }

    protected static class RespModel extends YxyApiRespModel {
        // ID
        public String id;
        // 对应班级圈ID
        public String momentsId;
        // 评论用户ID
        public String userId;
        // 评论用户名称
        public String userNick;
        // 评论人头像
        public String avatar;
        // 被回复人名称
        public String replyerNick;
        // 评论内容
        public String comment;
        // 评论时间
        public String dateFormat;
        // 是否自己的评论
        public boolean ownComment;

    }
}
